package com.example.android_wx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.example.android_wx.R;
import com.example.android_wx.base.BaseActivity;
import com.example.android_wx.http.BaseCallBack;
import com.example.android_wx.http.HttpHelp;
import com.google.android.material.snackbar.Snackbar;



public class RetrieveActivity extends BaseActivity {
    private AppCompatEditText mobileEditText;
    private AppCompatEditText passwordEditText;
    private AppCompatEditText nicknameEditText;
    private AppCompatTextView registerTextView;
    private View mobileView, passwordView, nicknameView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_retrieve);
        super.onCreate(savedInstanceState);


    }

    @Override
    public void initView() {
        mobileEditText = findViewById(R.id.mobileEditText);
        passwordEditText = findViewById(R.id.passwordEditText);
        nicknameEditText = findViewById(R.id.nicknameEditText);
        mobileView = findViewById(R.id.mobileView);
        passwordView = findViewById(R.id.passwordView);
        nicknameView = findViewById(R.id.nicknameView);
        registerTextView=findViewById(R.id.registerTextView);
    }

    @Override
    public void initEvent() {

        mobileEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.primary);
                passwordView.setBackgroundResource(R.color.textHint);
                nicknameView.setBackgroundResource(R.color.textHint);
            }
        });

        passwordEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.textHint);
                passwordView.setBackgroundResource(R.color.primary);
                nicknameView.setBackgroundResource(R.color.textHint);
            }
        });

        nicknameEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.textHint);
                passwordView.setBackgroundResource(R.color.textHint);
                nicknameView.setBackgroundResource(R.color.primary);
            }
        });


        registerTextView.setOnClickListener(view -> {
            String mobile = mobileEditText.getText().toString();
            String password = passwordEditText.getText().toString();
            String nickname = nicknameEditText.getText().toString();
            if (mobile.isEmpty()) {
                //吐司提醒 Snackbar;
                Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                Snackbar.make(registerTextView, "请输入手机号", Snackbar.LENGTH_SHORT).show();
                return;
            }
            if (nickname.isEmpty()) {
                //吐司提醒 Snackbar;
                Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show();
                return;
            }

            if (password.isEmpty()) {
                //吐司提醒 Snackbar;
                Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
                return;
            }
            //防止多次点击
            registerTextView.setEnabled(false);//禁用按钮
            registerTextView.setText("正在找回...");//修改按钮文字
            //发送注册请求,注意IP地址改成自己本机的IP地址
            HttpHelp.get().ready("base", "findPass")
                    .add("mobile", mobile)
                    .add("newpass", password)
                    .add("nickname", nickname)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            Toast.makeText(RetrieveActivity.this, "找回成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            finish();
                        }

                        @Override
                        public void onFailure(String reason) {
                            Toast.makeText(RetrieveActivity.this, reason, Toast.LENGTH_SHORT).show();
                            registerTextView.setEnabled(true);//启用按钮
                            registerTextView.setText("找回");//修改按钮文字
                        }
                    });

        });

    }



}